// $Id: nio.h 49 2011-04-18 16:51:06Z cedric.shih@gmail.com $
/*
 * Copyright (C) 2010 Cedric Shih <cedric.shih@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef NIO_H_
#define NIO_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <errno.h>

const char *nio_get_version(void);

enum nio_errno {
	NIO_EOK = 0, /**< Success. */
	NIO_EPERM = EPERM, /**< Operation not permitted. */
	NIO_EINVAL = EINVAL, /**< Invalid argument(s). */
	NIO_ENOMEM = ENOMEM, /**< Out of memory. */
	NIO_EOVERFLOW = EOVERFLOW, /**< Buffer overflows. */
	NIO_ERANGE = ERANGE, /**< Out of range. */
	NIO_EIO = EIO, /**< I/O error. */
	NIO_ECONNRESET = ECONNRESET, /**< Connection reset by peer. */
	NIO_EAGAIN = EAGAIN, /**< Try again later. */
	NIO_EEXIST = EEXIST, /**< Event pending. */
	NIO_ENOENT = ENOENT, /**< Event not pending. */
	NIO_EINPROGRESS = EINPROGRESS, /**< Channel message pending */
	NIO_EUNKNOWN = 0x8000, /**< Unknown error. */
	NIO_EUNDERFLOW, /**< Buffer underflows. */
	NIO_EOPENED, /**< Channel opened. */
	NIO_ECLOSED, /**< Channel closed. */
	NIO_ELOCK, /**< Concurrent failure. */
};

const char *nio_strerror(enum nio_errno err);

/**
 * @brief Log levels.
 */
enum niolog_level {
	NIO_TRACE, /**< Trivial step tracing log. */
	NIO_DEBUG, /**< Debugging log. */
	NIO_INFO, /**< Informative log. */
	NIO_WARN, /**< Warning log. */
	NIO_ERROR, /**< Error log. */
	NIO_FATAL, /**< Fatal error log. */
};

/**
 * @brief Prototype of logger function.
 *
 * @param level - log level
 * @param func - function name
 * @param line - line number
 * @param message - log message
 */
typedef void (*niologger_cb)(int level, const char *func, unsigned int line,
		const char *message, void *arg);

/**
 * @brief Set logger function.
 *
 * @param cb - logger function
 * @see niologger_cb
 */
void niologger_set(niologger_cb cb, void *arg);

struct nioradiator;

struct nioradiator *nioradiator_new(unsigned int concurrency);

void nioradiator_free(struct nioradiator *radiator);

int nioradiator_start(struct nioradiator *radiator);

int nioradiator_stop(struct nioradiator *radiator);

struct event_base;

struct nioreactor;

struct nioreactor *nioreactor_new(struct event_base *selector);

void nioreactor_free(struct nioreactor *reactor);

int nioreactor_loop(struct nioreactor *reactor,
		struct nioradiator *radiator);

int nioreactor_break(struct nioreactor *reactor);

struct event_base *nioreactor_get_selector(struct nioreactor *reactor);

struct nioradiator;

struct nioradiator *nioradiator_new(unsigned int level);

void nioradiator_free(struct nioradiator *rediator);

#ifdef __cplusplus
}
#endif

#endif /* NIO_H_ */
